<template>
    <div style="display: flex;justify-content: space-between;margin-top: 8px">
        <div>
            <el-button size="mini" type="primary" @click="saveAsCommonWords()">存为常用审批语</el-button>
        </div>
        <div style="display: flex;justify-content: space-between">
            <div style="padding-right: 8px;">
                <span>常用审批语: </span>
            </div>
            <div class="common_words_div">
                <el-select v-model="commonWordsDicItem"
                           placeholder="请选择">
                    <el-option v-for="item in commonWordsDic"
                               :key="item.id"
                               :label="item.content"
                               :value="item.content">
                    </el-option>
                </el-select>
            </div>
        </div>
    </div>
</template>

<script>
    import { mapGetters } from "vuex";
    import { getPresentDeptCommonWords,saveAsCommonWords } from "@/api/common";
    export default {
        name: "commonWords",
        created(){
            this.putCommonWords()
        },
        data() {
            return {
                commonWordsDic: [
                    {
                        'content':'测试1',
                        'id':1
                    },
                    {
                        'content':'测试2',
                        'id':2
                    },
                ],
                commonWordsDicItem:null,
                commitWordsForm:{
                    userId:null,
                    content:null,
                }
            }
        },
        computed: mapGetters([
            "userInfo"
        ]),
        props: {
            //当前输入框的语句
            presentWords:{
                type: String,
                default: null
            },
        },
        watch:{
            'commonWordsDicItem'(val){
                if(val != '' && val != null){
                    this.$emit('putCommonWordsToForm',val)
                }
            }
        },
        methods:{
            //存为审批常用语方法
            saveAsCommonWords(){
                if(this.presentWords==''||this.presentWords==null){
                    this.$notify({
                        title: "提醒",
                        type: "warning",
                        message: "内容不能为空",
                        duration: 2000
                    });
                    return
                }
                if(this.presentWords!=''&&this.presentWords!=null){
                    this.commitWordsForm.userId = this.userInfo.userId;
                    this.commitWordsForm.content = this.presentWords;
                    saveAsCommonWords(this.commitWordsForm).then(response =>{
                        if (response.data.code == 0) {
                            this.$notify({
                                title: '成功',
                                message: '添加成功',
                                type: 'success',
                                duration: 2000
                            });
                            this.putCommonWords()
                        }
                    })
                }

            },
            //得到当前用户的审批常用语
            putCommonWords(){
                getPresentDeptCommonWords(this.userInfo.userId).then(response =>{
                    if (response.data.code == 0) {
                        this.commonWordsDic=response.data.data;
                    }
                })
            },
        }


    }
</script>

<style lang="scss" scoped>
    .common_words_div{
        /deep/ .el-select{
            width: 330px;
        }
    }
</style>
